package com.zenithsun.common.image;

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Graphics;

import javax.imageio.ImageIO;

import com.zenithsun.common.util.image.ImageSize;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 图片剪切
 * @author wanglei@enersun.com.cn
 * @createTime 2016-7-11 12:23:26
 */
public class ImageCut {
	
	/**
	 * 剪切方法
	 * @param srcImageFile 源图像地址
	 * @param x 目标切片起点x坐标
	 * @param y 目标切片起点y坐标
	 * @param destWidth 目标切片宽度
	 * @param destHeight 目标切片高度
	 */
	public static void abscut(String srcImageFile, int x, int y, int destWidth,int destHeight) {
		try {
			Image img;
			ImageFilter cropFilter;
			// 读取源图像
			BufferedImage bi = ImageIO.read(new File(srcImageFile));
			// 源图宽度
			int srcWidth = bi.getWidth();
			// 源图高度
			int srcHeight = bi.getHeight();
			if (srcWidth >= destWidth && srcHeight >= destHeight) {
				Image image = bi.getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);
				/*
				 * 判断原图的宽高和DIV宽高的大小 根据图片外层DIV的宽度，选择的起始点则有相对变化
				 */
				int x1 = x * srcWidth / 420;
				int y1 = y * srcWidth / 420;
				int w = destWidth * srcWidth / 420;
				int h = destHeight * srcWidth / 420;

				// 四个参数分别为图像起点坐标和宽高
				// 即: CropImageFilter(int x,int y,int width,int height)
				cropFilter = new CropImageFilter(x1, y1, w, h);
				img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
				BufferedImage tag = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
				Graphics g = tag.getGraphics();
				g.drawImage(img, 0, 0, null); // 绘制缩小后的图
				g.dispose();
				// 输出为文件
				ImageIO.write(tag, "JPEG", new File(srcImageFile));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 /** 
     * 裁剪--指定坐标/大小 
     * @param srcUrl 源图片地址 
     * @param targetUrl 目标图片地址 
     * @param width 宽 
     * @param height 高 
     * @param pointA_1 坐标A1  
     * @param pointA_2坐标A2  
     * @param pointB_1坐标B1 
     * @param pointB_2坐标B2 
     * @throws IOException 
     */  
    public static void cut(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {  
    	File file=new File(targetUrl);
    	if(!file.getParentFile().exists()){
    		file.getParentFile().mkdirs();
    	}
    	Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).toFile(targetUrl);
    	//删除原来封面的图片
    	new File(srcUrl).delete();
    }
    
    /** 
     * 裁剪--指定坐标/大小 
     * @param srcUrl 源图片地址 
     * @param targetUrl 目标图片地址 
     * @param width 宽 
     * @param height 高 
     * @param pointA_1 坐标A1  
     * @param pointA_2坐标A2  
     * @param pointB_1坐标B1 
     * @param pointB_2坐标B2 
     * @throws IOException 
     */  
    public static void cutAndWater(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {  
    	File file=new File(targetUrl);
    	if(!file.getParentFile().exists()){
    		file.getParentFile().mkdirs();
    	}
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(width == 250){
    		watermarkImage = "250x167.png";
    	}else if(width == 400){
    		watermarkImage = "400x267.png";
    	}else if(width == 750){
    		watermarkImage = "750x500.png";
    	}else if(width == 1000){
    		watermarkImage = "1000x667.png";
    	}
    	System.out.println("水印地址:"+watermarkImage);
    	Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(path + watermarkImage)), 1f).outputQuality(0.8f).toFile(targetUrl);
    	//删除原来封面的图片
    	new File(srcUrl).delete();
    }
    
    
    
    public static void createImageBySize(String sourceUrl,String targetUrl,int width,int height)throws IOException{
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(width == 250){
    		watermarkImage = "250x167.png";
    	}else if(width == 400){
    		watermarkImage = "400x267.png";
    	}else if(width == 750){
    		watermarkImage = "750x500.png";
    	}else if(width == 1000){
    		watermarkImage = "1000x667.png";
    	}
    	System.out.println("水印地址:"+watermarkImage);
    	Thumbnails.of(sourceUrl).width(width).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(path + watermarkImage)), 1f).outputQuality(1.0f).toFile(targetUrl); 
    }
    
    public static void createImageBySize(byte[] bs,String targetUrl,ImageSize is)throws IOException{
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(is.getWidth() == 250){
    		watermarkImage = "250x167.png";
    	}else if(is.getWidth() == 400){
    		watermarkImage = "400x267.png";
    	}else if(is.getWidth() == 750){
    		watermarkImage = "750x500.png";
    	}else if(is.getWidth() == 1000){
    		watermarkImage = "1000x667.png";
    	}
    	InputStream inputStream=new ByteArrayInputStream(bs);
    	//Thumbnails.of(inputStream).size(200, 167);
    	Thumbnails.of(inputStream).width(is.getWidth()).height(is.getHeight()).outputQuality(1.0f).toFile(targetUrl);
    	inputStream.close();
    }
    /**
     * 创建图片并加水印
     * @param bs
     * @param sourceUrl
     * @throws IOException
     */
    public static void createAddWater(byte[] bs,File sourceUrl)throws IOException{
    	ImageSize is=getImageSize(bs);
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(is.getWidth() >= 250&&is.getWidth()<400){
    		watermarkImage = "250x167.png";
    	}else if(is.getWidth() >= 400&&is.getWidth()<750){
    		watermarkImage = "400x267.png";
    	}else if(is.getWidth() >= 750&&is.getWidth()<1000){
    		watermarkImage = "750x500.png";
    	}else if(is.getWidth() >= 1000){
    		watermarkImage = "1000x667.png";
    	}
    	InputStream inputStream=new ByteArrayInputStream(bs);
    	System.out.println(sourceUrl.getPath());
    	Thumbnails.of(inputStream).width(is.getWidth()).height(is.getHeight()).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(path + watermarkImage)), 1f).outputQuality(1.0f).toFile(sourceUrl);
    	inputStream.close();
    }
    
    /**
     * 创建图片并加水印
     * @param bs
     * @param sourceUrl
     * @throws IOException
     */
    public static void createAddWater(byte[] bs,File sourceUrl,int width)throws IOException{
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(width == 250){
    		watermarkImage = "250x167.png";
    	}else if(width == 400){
    		watermarkImage = "400x267.png";
    	}else if(width == 750){
    		watermarkImage = "750x500.png";
    	}else if(width == 1000){
    		watermarkImage = "1000x667.png";
    	}
    	InputStream inputStream=new ByteArrayInputStream(bs);
    	System.out.println(sourceUrl.getPath());
    	Thumbnails.of(inputStream).width(width).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(path + watermarkImage)), 1f).outputQuality(1.0f).toFile(sourceUrl);
    	inputStream.close();
    }
    
    
    /**
     * 给目标图片加水印
     * @param sourceUrl
     * @throws IOException
     */
    public static void addWater(String sourceUrl)throws IOException{
    	ImageSize is=getImageSize(sourceUrl);
    	String path = ImageCut.class.getClassLoader().getResource("").getPath();
        path = path.substring(1,path.indexOf("WEB-INF")) + "zenithsun/images/watermark/";
    	String watermarkImage = "250x167.png";
    	if(is.getWidth() >= 250&&is.getWidth()<400){
    		watermarkImage = "250x167.png";
    	}else if(is.getWidth() >= 4000&&is.getWidth()<750){
    		watermarkImage = "400x267.png";
    	}else if(is.getWidth() >= 750&&is.getWidth()<1000){
    		watermarkImage = "750x500.png";
    	}else if(is.getWidth() >= 1000){
    		watermarkImage = "1000x667.png";
    	}
    	System.out.println("水印地址:"+watermarkImage);
    	Thumbnails.of(sourceUrl).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(path + watermarkImage)), 1f).outputQuality(1.0f).toFile(sourceUrl); 
    }
    
    /**
     * 获取图片的宽和高
     * @param imagePath
     * @return
     * @throws IOException 
     */
    public static ImageSize getImageSize(String imagePath) throws IOException{
    	System.out.println(imagePath);
    	InputStream ist = new FileInputStream(imagePath);//通过文件名称读取
    	BufferedImage bufferedImage = ImageIO.read(ist);
    	ImageSize is=new ImageSize(bufferedImage.getWidth(),bufferedImage.getHeight());
    	bufferedImage.flush();
    	ist.close();
    	return is;
    }
    
    /**
     * 获取图片的宽和高
     * @param imagePath
     * @return
     * @throws IOException 
     */
    public static ImageSize getImageSize(File imagePath) throws IOException{
    	System.out.println(imagePath);
    	InputStream ist = new FileInputStream(imagePath);//通过文件名称读取
    	BufferedImage bufferedImage = ImageIO.read(ist);
    	ImageSize is=new ImageSize(bufferedImage.getWidth(),bufferedImage.getHeight());
    	bufferedImage.flush();
    	ist.close();
    	return is;
    }
    
    /**
     * 获取图片的宽和高
     * @param imagePath
     * @return
     * @throws IOException 
     */
    public static ImageSize getImageSize(byte[] bs) throws IOException{
    	InputStream ist = new ByteArrayInputStream(bs);//通过文件名称读取
    	BufferedImage bufferedImage = ImageIO.read(ist);
    	ImageSize is=new ImageSize(bufferedImage.getWidth(),bufferedImage.getHeight());
    	bufferedImage.flush();
    	ist.close();
    	return is;
    }
}
